package com.uit.trip_.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.uit.trip_.TripApplication;
import com.uit.trip_.models.LocationModel;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class LocationService extends Service {

	private LocationManager locationManager;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.i("duongnx", "location service");

		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Location location = locationManager
				.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		locationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, 180000, 10, listener);
		setLocationToApplication(location);
	}

	private LocationListener listener = new LocationListener() {

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub

		}

		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub

		}

		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub

		}

		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			setLocationToApplication(location);
		}
	};

	public void setLocationToApplication(Location location) {
		String address = getMyAddress(location);
		Log.i("duongnx", "address:" + address);
		LocationModel locationModel = new LocationModel(location, address);
		TripApplication app = (TripApplication) getApplication();
		app.setLocation(locationModel);
	}

	private String getMyAddress(Location location) {
		if (location == null)
			return "";
		StringBuilder myAddress = new StringBuilder();
		Double la = location.getLatitude();
		Double lng = location.getLongitude();
		Geocoder gc = new Geocoder(this, Locale.getDefault());
		List<Address> addresses = new ArrayList<Address>();
		try {
			addresses = gc.getFromLocation(la, lng, 5);
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("duongnx", "error:" + e);
		}

		if (addresses.size() != 0) {
			Address address = addresses.get(0);
			for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
				if (i != address.getMaxAddressLineIndex() - 1) {
					myAddress.append(address.getAddressLine(i)).append(", ");
				} else {
					myAddress.append(address.getAddressLine(i));
				}
			}
		}
		if (myAddress == null || myAddress.equals("")) {
			return "Not found";
		}
		return myAddress.toString();
	}

}
